package com.xxx.mq;


import com.xxx.service.IPayOrderService;
import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;

import java.util.HashMap;

/**
 * 处理支付结果的本地事务,修改支付单状态,保存流水
 */
@RocketMQTransactionListener(txProducerGroup = "mq_consumer_group_pay_notify"
)
public class MQTXPay implements RocketMQLocalTransactionListener {
    @Autowired
    private IPayOrderService payOrderService;

    /**
     * 修改支付单,保存流水
     */
    @Override
    public RocketMQLocalTransactionState executeLocalTransaction(Message message, Object arg) {
        try {
            HashMap<String, Object> map = (HashMap<String, Object>) arg;
            payOrderService.updatePayOrderAndSavePayFlow(map);
            return RocketMQLocalTransactionState.COMMIT;
        } catch (Exception e) {
            e.printStackTrace();
            return RocketMQLocalTransactionState.ROLLBACK;
        }
    }

    @Override
    public RocketMQLocalTransactionState checkLocalTransaction(Message message) {
        return null;
    }
}
